<%@ page language="java" import="java.util.*,com.zjf.dao.*,com.zjf.entity.*,com.zjf.dao.impl.*" pageEncoding="utf-8"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.File"%>
<%@page import="com.zjf.util.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ include file="config/config.jsp" %>
<%@ include file= "config/checkInstalled.jsp"%>
<%@ include file= "loadDatas.jsp"%>
<%!
	List<Article> articles = new ArrayList<Article>();
	List<User> users = new ArrayList<User>();
	List<Review> reviews = new ArrayList<Review>();
	//页数
	int p;
	//每页显示5个
	int pageArticles;
	//总页数
	int pageTotal;
	//登录用户
	User loginUser;
	//文章显示字数
	int contentLimit;
	UserDao daou;
	//获得归档文章
	public ArrayList<String> getLoadArticles(){
	ArrayList<String> loads = new ArrayList<String>();
		try{
		
		for(Article a:articles){
		SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
			Date d = f.parse(a.getPubDate());
			String load = "<a href=articles.jsp?load="+(d.getYear()+1900)+"0"+(d.getMonth()+1)+">"+(d.getYear()+1900)+"年"+(d.getMonth()+1)+"月"+"</a>";
			if(!loads.contains(load)){
				loads.add(load);
			}
		}
		return loads;
		}catch(Exception e){
			e.printStackTrace();
		}
		return loads;
	}
	//获得类别文章
	public ArrayList<String> getTypeArticles(){
	ArrayList<String> types = new ArrayList<String>();
		try{
		
		for(Article a:articles){
			String t = a.getTitle();
			int beginIndex = t.indexOf("[");
			int endIndex =t.indexOf("]");
			if(beginIndex!=-1 && endIndex!=-1){
				t = t.substring(beginIndex+1,endIndex);
				/**获取类别文章数*/
				String c = URLEncoder.encode(t,"gbk");
				//c=new String(c.getBytes("ISO-8859-1"));
				String type = "<a href=articles.jsp?type="+c+">"+t+"</a>";
				if(!types.contains(type)){
					types.add(type);
				}
			}
		}
		return types;
		}catch(Exception e){
			e.printStackTrace();
		}
		return types;
	}
%>
<%
	try{
		loginUser = (User)session.getAttribute("loginUser");
		if(application.getAttribute("articles")==null){
			ArticleDao dao = new ArticleDaoImpl();
			articles = dao.queryArticles();
			application.setAttribute("articles",articles);
		}else{
			
			articles = (ArrayList)application.getAttribute("articles");
		}
		daou= new UserDaoImpl();
		users=daou.queryAllUsers();
		ReviewDao daor = new ReviewDaoImpl();
		reviews = daor.queryReviews();
		pageArticles = blog_config.getInt("pageArticles");
		contentLimit =blog_config.getInt("articleWords");
		pageTotal = articles.size()%pageArticles==0?articles.size()/pageArticles:articles.size()/pageArticles+1;
		p = Integer.parseInt(request.getParameter("page"));
		if(p<1||p>pageTotal){
			p = 1;
		}
		
	}catch(Exception e){
		p=1;
		
	}
%>
<html>
<head>

<title><%=blog_config.get("siteTitle") %></title>


<style type="text/css">
<!--
.STYLE3 {color: #FFFFFF}
.STYLE5 {
	font-size: 10px;
	color: #FF0000;
	font-weight: bold;
}
.STYLE7 {font-size: 12px}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

					function move(uheadurl,id,uname,una,up,usex,uartnum,urewnum,uemail,uregt){
					var objDiv = document.getElementById("pop");
					
					  
					  tempX   =   event.clientX   +   document.body.scrollLeft; 
					  tempY   =   event.clientY   +   document.body.scrollTop; 
					  objDiv.style.left = tempX + 1;
					  objDiv.style.top = tempY + 1;
					  //alert(uheadurl);
					  document.getElementById("uheadurl").innerHTML = "<img src="+decodeURI(uheadurl)+" width=100 height=100/>";
					 document.getElementById("uid").innerHTML = ""+id;
					 document.getElementById("uname").innerHTML = ""+uname;
					 document.getElementById("una").innerHTML = ""+una;
					 document.getElementById("up").innerHTML = "<b>"+up+"</b>";
					 document.getElementById("usex").innerHTML = ""+usex;
					 document.getElementById("uartnum").innerHTML = ""+uartnum;
					 document.getElementById("urewnum").innerHTML = ""+urewnum;
					 document.getElementById("uemail").innerHTML = ""+uemail;
					 document.getElementById("uregt").innerHTML = ""+uregt;
					  objDiv.style.display = '';
					   
					    
					}
					function out(){
					var objDiv = document.getElementById("pop");
					  objDiv.style.display = 'none';
					  
					}
					function moveIn(){
						var objDiv = document.getElementById("pop");
						objDiv.style.display = '';
					}
				</script> 
</head>


<link rel="stylesheet" type="text/css" href="style/style.css" />
<center>
<body>
<div onMouseMove="moveIn();" onMouseOut="out();" align="left" style="filter:alpha(opacity=90); padding:10px 10px 10px 10px; position: absolute; color: #000000; display:none; background-color:#E1E1E1; border: 1px solid #CCCCCC; font-size:13px;" id="pop">
					<span id="uheadurl"></span><br/>
					ID：<span id="uid"></span><br/>
					用户名：<span id="uname"></span><br/>
					昵称：<span id="una"></span><br/>
					头衔：<span id="up"></span><br/>
					性别:<span id="usex"></span><br/>
					文章数：<span id="uartnum"></span><br/>
					评论数：<span id="urewnum"></span><br/>
					邮箱：<span id="uemail"></span><br/>
					注册时间：<span id="uregt"></span><br/>
				</div>	


 
  
  <div id ="wrap">
		
	
			<jsp:include page="head.jsp"></jsp:include>
			<font size=2>欢迎您<%=loginUser==null?"":":"+loginUser.getUsername() %>，您的身份是：<%=loginUser==null?"游客":User.permissonName[loginUser.getPermisson()] %>		    </font>
			&nbsp;<%=loginUser!=null?"<a href='userMg.jsp?id="+loginUser.getId()+"'>控制面板</a>":"" %><%=loginUser!=null&&loginUser.getPermisson()==2?"|<a href='admin/index.jsp'>管理后台</a>":"" %>
			<div id="right_pad1">
		    <div class="STYLE3" id="title">
		      文章类别	        </div>
			<%for(String s:getTypeArticles()){ %>
			<p><%=s %></p>
			<%} %>
		  </div>
			
			<div id="right_pad1">
		    <div class="STYLE3" id="title">
		      文章归档	        </div>
			<%for(String s:getLoadArticles()){ %>
			<p><%=s %></p>
			<%} %>
		  </div>
			<div id="right_pad1">
					<div id="title">
					  最新文章	        </div>
					
					<ul>
					  <%for(int i=0;i<((articles.size()<10)?articles.size():10);i++){ %>
					  <li><a href="article-<%=articles.get(i).getId() %>.html"><%=articles.get(i).getTitle() %></a></li>
					  <%} %>
					 
			  </ul>
			</div>
			<div id="right_pad1">
		    <div id="title">
		      最新评论	        </div>
			
	        <ul>
	           <%for(int i=0;i<((reviews.size()<10)?reviews.size():10);i++){ %>
	          <li><a href="article-<%=reviews.get(i).getRid() %>.html"><%=reviews.get(i).getAuthor()%>:<%=reviews.get(i).getContent() %></a></li>
	           <%} %>
			  </ul>
			</div>
			<div id="right_pad1">
		    <div id="title">
		      最新注册用户	        </div>
			
	        <ul>
	          
	          <%for(int i=0;i<((users.size()<10)?users.size():10);i++){ %>
	          <li><%=users.get(i).getUsername()%></li>
	           <%} %>
			  </ul>
			</div>
			<div id="left_pad1">
			<div id="content"><jsp:include page="introduce.html" /></div>
			</div>
		  <div id="left_pad1">博文区-文章数：<%=articles.size() %>
		  <form name= "page1" action="index.jsp">
							
			页码：
			  <select name="page" onChange="document.page1.submit()">
                <% 
								for(int i=1;i<=pageTotal;i++){
							%>
                <option value=<%=i%>   <%=p==i?"selected='selected'":"" %>> 第<%=i %>页 </option>
                <%} %>
              </select> 
					</form>
		  <%for(int i=(p-1)*pageArticles;i<(p==pageTotal?articles.size():p*pageArticles);i++){ %>
				<%User u = daou.queryUser(articles.get(i).getAuthor_id());
				 %>
				<div id="content">
				
				
				<table>
				<tr border="1" bordercolor="#CCCCCC">
				<%String headUrl = URLEncoder.encode(u.getHeadUrl()); %>
				<img id="headimg" width=50 height=50 onMouseMove="move('<%=headUrl%>','<%=u.getId() %>','<%=u.getUsername() %>','<%=u.getName() %>','<%=User.permissonName[u.getPermisson()] %>','<%=User.sexName[u.getSex()] %>','<%=u.getArticleNum() %>','<%=u.getReviewNum() %>','<%=u.getEmail() %>','<%=u.getRegDate().substring(0,u.getRegDate().lastIndexOf(" ")) %>');" onMouseOut="out();" src=<%=u.getHeadUrl() %> /><font size="2" color=blue><br><b><%=articles.get(i).getAuthor() %>
</b></font>
				</tr>
				</table>
				<center><b><%=articles.get(i).getTitle() %> </b></center>
				<center>日期:<%=articles.get(i).getPubDate() %></center><br>
				    <%
				    	StringBuilder br = new StringBuilder();
				    		    	int len =articles.get(i).getContent().length();
				    		    	if(len<=0) br.append("...");                          
				    		      	else {
				    		      	int rest = len-contentLimit;
				    		      	br.append(articles.get(i).getContent().substring(0,contentLimit>len?len-1:contentLimit-1));
				    			      	if(rest>0){
				    			      		br.append("<font size =2>...<省略>"+rest+"字符</font>");
				    			      	}
				    		     	
				    		     	}
				    		     	
				    		     	String scontent =new HtmlRegexpUtil().filterHtml(br.toString());
				    %>
				    <%=scontent%><br>
				    <%
				    String del = "";
				    String mod = "";
				    if(loginUser!=null && loginUser.getPermisson()==2){
				    	del+="|<a href='delete.do?id="+articles.get(i).getId()+"'>删除</a>";
				    	
				    } 
				    if(loginUser!=null){
				    	if(loginUser.getPermisson()==2||loginUser.getId()==articles.get(i).getAuthor_id()){
				    		mod+="|<a href='modiArticle.jsp?id="+articles.get(i).getId()+"'>编辑</a>";
				    	}
				    }
				    %>
				<p align="right" class="STYLE7"><a href="article-<%=articles.get(i).getId() %>.html">查看全文(<%=articles.get(i).getViewCount() %>)</a>|评论(<%=articles.get(i).getReviewCount() %>)<%=mod%><%=del%></p>
		    </div>
	  		<%} %>		
				  	
					<p>&nbsp;</p>
					<p>&nbsp;</p>	
					<form name= "page" action="index.jsp">
							
			页码：
			  <select name="page" onChange="document.page.submit()">
                <% 
								for(int i=1;i<=pageTotal;i++){
							%>
                <option value=<%=i%>   <%=p==i?"selected='selected'":"" %>> 第<%=i %>页 </option>
                <%} %>
              </select> 
					</form>
				    
	</div>
	        
			<jsp:include page="foot.jsp"></jsp:include>
  </div>
</body>
</center>
</html>
